//


#import "MB_AddDietViewController.h"
#import "MB_AddDietCollectionViewCell.h"

@interface MB_AddDietViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,MB_AddDietCollectionDelegate>
/** */
@property (nonatomic, strong) UICollectionView *collectionView;
/** */
@property (nonatomic, strong) MB_SqlView *sql;
/** 早餐*/
@property (nonatomic, copy) NSString *breakfastS;
/** e中餐*/
@property (nonatomic, copy) NSString *chineseMealS;
/** 晚餐*/
@property (nonatomic, copy) NSString *dinnerS;
/** 其他*/
@property (nonatomic, copy) NSString *otrerS;

@end

@implementation MB_AddDietViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _sql = [[MB_SqlView alloc]initWithData:@"DietData" dataName:@"MoodData" success:^{} fail:^(NSError * _Nullable error) {}];
    [self setnavBatalweeTitle:@"Add Diet"];
    self.view.backgroundColor = RGB(241 ,241 ,241);
    [self.view addSubview:self.collectionView];
    [self setBtn];
}//MARK:btn
- (void)setBtn {
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(_collectionView.frame)+50, kMainScreenWidth-100, 55)];
    [button setTitle:@"Submit" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:FontPFRegular(kRHeight(20))];
    button.layer.cornerRadius = 6;
    [button setBackgroundColor:RGB(186, 85, 211)];
    [button addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    WTB_HomeDPicModel * model = _dataSource[indexPath.row];
    
    MB_AddDietCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MB_AddDietCollectionViewCell class]) forIndexPath:indexPath];
    cell.indexx = indexPath.row;
    cell.delegate = self;
    cell.model = _model;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake( (kMainScreenWidth - 15)/2, (kMainScreenWidth - 15)/2 );
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerV" forIndexPath:indexPath];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, kMainScreenWidth, kRHeight(20))];
    label.textColor = rgba(90, 90, 90, 1);
    label.font = FontPFRegular(kRHeight(15));
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"Did you have dinner today? What did you eat?";
    [view addSubview:label];
    
    return view;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kMainScreenWidth, 50);
}
//MARK:cell代理
- (void)senderDietCell:(MB_AddDietCollectionViewCell *)cell text:(NSString *)text {
    switch (cell.indexx) {
        case 0:
            _breakfastS = text;
            break;
        case 1:
            _chineseMealS = text;
            break;
        case 2:
            _dinnerS = text;
            break;
        case 3:
            _otrerS = text;
            break;
        default:
            break;
    }
}//MARK:
- (void)Action {
    NSLog(@"jkddsff");
    //
    NSDateFormatter *nowFormate = [[NSDateFormatter alloc] init];
    nowFormate.dateFormat = @"yyyy-MM-dd";
    NSString *nowTime = [nowFormate stringFromDate:[NSDate date]];
    //
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:_breakfastS = _breakfastS.length == 0 ? @"":_breakfastS forKey:@"breakfast"];
    [dic setObject:_chineseMealS = _chineseMealS.length == 0 ? @"":_chineseMealS forKey:@"chineseMeal"];
    [dic setObject:_dinnerS = _dinnerS.length == 0 ? @"":_dinnerS forKey:@"dinner"];
    [dic setObject:_otrerS = _otrerS.length == 0 ? @"":_otrerS forKey:@"other"];
    //
    if (_model == nil) {
        //
        [dic setObject:nowTime forKey:@"date"];
        [dic setObject:[self randomnum] forKey:@"iD"];
        [_sql mb_insertNewEntity:dic success:^{
            [self.navigationController popViewControllerAnimated:YES];
        } fail:^(NSError * _Nullable error) {}];
    }
    else {
        //
        [_sql mb_updateEntity:dic key:[NSString stringWithFormat:@"iD=%@",_model.iD] success:^{
            [self.navigationController popViewControllerAnimated:YES];
        } fail:^(NSError * _Nullable error) {}];
    }
    
}



//MARK:懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        //
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight - UI_NAV_BAR_HEIGHT-UI_TAB_BAR_HEIGHT-90) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
        _collectionView.backgroundColor =  RGB(241 ,241 ,241);
        [_collectionView registerClass:[MB_AddDietCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MB_AddDietCollectionViewCell class])];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerV"];
    }
    return _collectionView;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
//
- (NSString*)randomnum {
    const int count = 6;
    NSString * source =@"125678934";
    NSMutableString *str=[[NSMutableString alloc]init];
    for (int i = 0; i < count; i++) {
        unsigned index=rand()%[source length];
        NSString * s=[source substringWithRange:NSMakeRange(index, 1)];
        [str appendString:s];
    }
    NSLog(@"-%@",str);
    return str;
}

@end
